package com.flc.gateway;

import com.flc.gateway.cmdqueue.CmdQueue;
import com.flc.gateway.config.AppConfig;
import com.flc.gateway.dataqueue.DiagnosisQueue;
import com.flc.gateway.dataqueue.LoginQueue;
import com.flc.gateway.dataqueue.ReportQueue;
import com.flc.gateway.dataqueue.TrajectoryQueue;
import com.flc.gateway.mq.MqConsumer;
import com.flc.gateway.mq.MqProducer;
import com.flc.gateway.redis.RedisPool;
import com.flc.gateway.server.Server;

/**
 * @author haizi
 * @date 2018/1/2
 */
public class Gateway {

    public static LoginQueue loginQueue;
    public static TrajectoryQueue trajectoryQueue;
    public static ReportQueue reportQueue;
    public static DiagnosisQueue diagnosisQueue;

    public static CmdQueue cmdQueue;

    public static void main(String[] args) throws Exception {

        Server server = new Server(AppConfig.getInstance().getNettyCfg());

        AppConfig.QueueCfg queueCfg = AppConfig.getInstance().getQueueCfg();
        AppConfig.RocketMqCfg rocketMqCfg = AppConfig.getInstance().getRocketMqCfg();
        AppConfig.RedisCfg redisCfg = AppConfig.getInstance().getRedisCfg();

        /* --- 初始化RocketMQ（消费者生产者单例） --- */
        MqConsumer.start(rocketMqCfg);
        MqProducer.init(rocketMqCfg);
        RedisPool.init(redisCfg);

        /* --- 初始化所需的队列 --- */
        loginQueue = new LoginQueue(queueCfg.getLoginLength(), rocketMqCfg.getLoginTopic());
        trajectoryQueue = new TrajectoryQueue(queueCfg.getLoginLength(), rocketMqCfg.getTrajectoryTopic());
        reportQueue = new ReportQueue(queueCfg.getLoginLength(), rocketMqCfg.getReportTopic());
        diagnosisQueue = new DiagnosisQueue(queueCfg.getLoginLength(), rocketMqCfg.getDiagnosisTopic());

        cmdQueue = new CmdQueue(queueCfg.getCmdLength(), rocketMqCfg);

        /* --- 开启相关数据队列的消费 --- */
        loginQueue.start();
        trajectoryQueue.start();
        reportQueue.start();
        diagnosisQueue.start();

        /* ---开启相关命令队列的消费 --- */
        cmdQueue.start();

        /* --- 开启数据接收服务 --- */
        server.run();
    }
}
